package com.test.image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class ImageWord {

    public void addWaterMarkByResource(String resourcePath, String tarImgPath, String waterMarkContent, Color markContentColor, Font font, int x, int y) {
        try {
            System.out.println("addWaterMarkByResource");
            // 读取原图片信息
            InputStream imageStream = this.getClass().getResourceAsStream(resourcePath);
            Image srcImg = ImageIO.read(imageStream);//文件转化为图片
            System.out.println("读取原图成功");
            addWaterMark(srcImg, tarImgPath, waterMarkContent, markContentColor, font, x, y);

        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("addWaterMarkByResource error:" + e.getLocalizedMessage());
        }

    }

    /**
     * @param srcImgPath       源图片路径
     * @param tarImgPath       保存的图片路径
     * @param waterMarkContent 水印内容
     * @param markContentColor 水印颜色
     * @param font             水印字体
     */
    public void addWaterMark(String srcImgPath, String tarImgPath, String waterMarkContent, Color markContentColor, Font font, int x, int y) {

        try {
            // 读取原图片信息
            //this.getClass().getResourceAsStream("")
            File srcImgFile = new File(srcImgPath);//得到文件
            Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
            addWaterMark(srcImg, tarImgPath, waterMarkContent, markContentColor, font, x, y);

        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("addWaterMark1 error:" + e.getLocalizedMessage());
        }
    }

    public void addWaterMark(Image srcImg, String tarImgPath, String waterMarkContent, Color markContentColor, Font font, int x, int y) {
        try {
            System.out.println("addWaterMark111111111111");
            if (srcImg == null) {
                System.out.println("addWaterMark srcImg is null");
            }
            int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
            int srcImgHeight = srcImg.getHeight(null);//获取图片的高

            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();

            // 设置图片背景色是透明的，否则生成的图片可能会出现黑边
            bufImg = g.getDeviceConfiguration().createCompatibleImage(srcImgWidth, srcImgHeight, Transparency.TRANSLUCENT);
            g.dispose();
            g = bufImg.createGraphics();
            //-------

            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            g.setColor(markContentColor); //根据图片的背景设置水印颜色
            g.setFont(font);              //设置字体

            // 使文字显示更清晰，否则文字可能模糊
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
            System.out.println("setfont22222222");
            //设置水印的坐标
            //int x = srcImgWidth - 2*getWatermarkLength(waterMarkContent, g);
            //int y = srcImgHeight - 2*getWatermarkLength(waterMarkContent, g);
            if (x == -1) {
                x = (srcImgWidth - getWatermarkLength(waterMarkContent, g)) / 2;
            }
            if (y == -1) {
                y = (srcImgHeight - getWatermarkHeight(waterMarkContent, g)) / 2;
            }
            g.drawString(waterMarkContent, x, y);  //画出水印
            g.dispose();

            System.out.println("tarImgPath:" + tarImgPath);
            int c = tarImgPath.lastIndexOf("/");
            String path = tarImgPath.substring(0, c + 1);
            System.out.println("temppath:" + path);
            File imgFile = new File(path);
            if (!imgFile.exists()) {
                System.out.println("addWaterMark 创建文件夹");
                imgFile.mkdirs();
            }

            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
            ImageIO.write(bufImg, "png", outImgStream);
            System.out.println("添加水印完成");
            outImgStream.flush();
            outImgStream.close();

        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("addWaterMark2 error:" + e.getLocalizedMessage());
        }

    }

    public int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }

    public int getWatermarkHeight(String waterMarkContent, Graphics2D g) {
        int p = g.getFontMetrics(g.getFont()).getHeight();
        return p;
    }

    public static void main(String[] args) {
        String src = "E:\\data\\desktop\\客户协议图片\\p.png";
        String target = "E:\\data\\desktop\\客户协议图片\\person.png";

        ImageWord iw = new ImageWord();
        iw.addWaterMark(src, target, "test", Color.BLACK, new Font("宋体",Font.PLAIN,50), 100, 100);
    }
}
